package com.xinke.module.system.controller.admin.oauth2;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.oauth2.*;
import com.xinke.module.system.model.entity.oauth2.OAuth2ApproveDO;
import com.xinke.module.system.service.oauth2.OAuth2ApproveService;

@Tag(name = "管理后台 - OAuth2 批准")
@RestController
@RequestMapping("/system/O-auth2-approve")
@Validated
public class OAuth2ApproveController {

    @Resource
    private OAuth2ApproveService oAuth2ApproveService;

    @PostMapping("/create")
    @Operation(summary = "创建OAuth2 批准")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-approve:create')")
    public Result<Long> createOAuth2Approve(@Valid @RequestBody OAuth2ApproveSaveReqVO createReqVO) {
        return success(oAuth2ApproveService.createOAuth2Approve(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新OAuth2 批准")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-approve:update')")
    public Result<Boolean> updateOAuth2Approve(@Valid @RequestBody OAuth2ApproveSaveReqVO updateReqVO) {
        oAuth2ApproveService.updateOAuth2Approve(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除OAuth2 批准")
    @Parameter(name = "approveId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:O-auth2-approve:delete')")
    public Result<Boolean> deleteOAuth2Approve(@RequestParam("approveId") Long approveId) {
        oAuth2ApproveService.deleteOAuth2Approve(approveId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得OAuth2 批准")
    @Parameter(name = "approveId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-approve:query')")
    public Result<OAuth2ApproveRespVO> getOAuth2Approve(@RequestParam("approveId") Long approveId) {
        OAuth2ApproveDO oAuth2Approve = oAuth2ApproveService.getOAuth2Approve(approveId);
        return success(BeanUtils.toBean(oAuth2Approve, OAuth2ApproveRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得OAuth2 批准分页")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-approve:query')")
    public Result<PageResult<OAuth2ApproveRespVO>> getOAuth2ApprovePage(@Valid OAuth2ApprovePageReqVO pageReqVO) {
        PageResult<OAuth2ApproveDO> pageResult = oAuth2ApproveService.getOAuth2ApprovePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OAuth2ApproveRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出OAuth2 批准 Excel")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-approve:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOAuth2ApproveExcel(@Valid OAuth2ApprovePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OAuth2ApproveDO> list = oAuth2ApproveService.getOAuth2ApprovePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "OAuth2 批准.xls", "数据", OAuth2ApproveRespVO.class,
                        BeanUtils.toBean(list, OAuth2ApproveRespVO.class));
    }

}